﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Order;

namespace OrderWebApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class OrdersController : Controller
    {
        private readonly OrderService _context;

        public OrdersController(OrderService context)
        {
            _context = context;
        }

        // GET: Orders
        [HttpGet("{id}")]
        public async Task<IActionResult> Index(string id)
        {
            if (id == null)
            {
                return Ok(await _context.InquiryOrdersAsync(null, OrderService.InquiryType.All));
            }
            return Ok(await _context.InquiryOrdersAsync(id, OrderService.InquiryType.OrderID));
        }

        // GET: Orders/Details/5
        //public async Task<IActionResult> Details(string id)
        //{
        //    if (id == null)
        //    {
        //        return NotFound();
        //    }

        //    var order = await _context.Orders
        //        .FirstOrDefaultAsync(m => m.OrderID == id);
        //    if (order == null)
        //    {
        //        return NotFound();
        //    }

        //    return View(order);
        //}

        // GET: Orders/Create
        //public IActionResult Create()
        //{
        //    return View();
        //}

        // POST: Orders/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        public async Task<IActionResult> Create([Bind("OrderID,OrderTime,Total,OrderDetails,TheCustomer")] Order.Order order)
        {
            if (ModelState.IsValid)
            {
                order.ComputeTotal();
                await _context.AddOrder(order);
                return CreatedAtAction(nameof(Create), order);
            }
            return NotFound("Model invalid.");
        }

        // GET: Orders/Edit/5
        [HttpPut("{id}")]
        public async Task<IActionResult> Edit(string id, [Bind("OrderID,OrderTime,Total,OrderDetails,TheCustomer")] Order.Order order)
        {
            if (id == null)
            {
                return NotFound();
            }
            if (order == null)
            {
                return NotFound();
            }
            await _context.ModifyOrder(id, order);
            return NoContent();
        }

        // POST: Orders/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public async Task<IActionResult> Edit(string id, [Bind("OrderID,OrderTime,Total")] Order.Order order)
        //{
        //    if (id != order.OrderID)
        //    {
        //        return NotFound();
        //    }

        //    if (ModelState.IsValid)
        //    {
        //        try
        //        {
        //            _context.Update(order);
        //            await _context.SaveChangesAsync();
        //        }
        //        catch (DbUpdateConcurrencyException)
        //        {
        //            if (!OrderExists(order.OrderID))
        //            {
        //                return NotFound();
        //            }
        //            else
        //            {
        //                throw;
        //            }
        //        }
        //        return RedirectToAction(nameof(Index));
        //    }
        //    return View(order);
        //}

        // GET: Orders/Delete/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(string id)
        {
            if (id == null)
            {
                return NotFound();
            }

            await _context.DeleteOrder(id);
            return NoContent();
        }

        // POST: Orders/Delete/5
        //[HttpPost, ActionName("Delete")]
        //[ValidateAntiForgeryToken]
        //public async Task<IActionResult> DeleteConfirmed(string id)
        //{
        //    var order = await _context.Orders.FindAsync(id);
        //    _context.Orders.Remove(order);
        //    await _context.SaveChangesAsync();
        //    return RedirectToAction(nameof(Index));
        //}

        //private bool OrderExists(string id)
        //{
        //    return _context.Orders.Any(e => e.OrderID == id);
        //}
    }
}
